#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
	char lines[80];

	if(argc != 6) {
		fprintf(stderr, "You need to give 5 arguments\n");
		return 1;
	}

	FILE *in; 
	if (!(in = fopen("spooky.csv", "r"))) {
		fprintf(stderr, "Can't open the file.\n");
		return 1;
	}
	FILE *file1 = fopen(argv[2], "w");
	FILE *file2 = fopen(argv[4], "w");
	FILE *file3 = fopen(argv[5], "w");

	while (fscanf(in, "%79[^\n]\n", lines) == 1) {
		if (strstr(lines, argv[1])) {
			fprintf(file1, "%s\n", lines);
		} else if (strstr(lines, argv[3])) {
			fprintf(file2, "%s\n", lines);
		} else {
			fprintf(file3, "%s\n", lines);
		}
	}
	
	fclose(file1);
	fclose(file2);
	fclose(file3);
	return 0;
}

